import { searchCover, isString, isNumber, multipleChecked, radioChecked, isArray, guID } from "@/common.js"
// 调整字段值
export const changeField = function (e, field) {
  e = e || ''
  const value = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  // 更新右侧的值
  const item = {...this.item, [field]: value}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [field]: value})) : v.children}))
  this.$emit('setFormData', formData)
  // console.log(formData)
}
// 选择操作属性
export const checkOperate = function (optItem) {
  const value = !optItem.isChecked
  this.operateList = multipleChecked(this.operateList, {id: optItem.id})
  // 更新右侧的值
  const item = {...this.item, [optItem.field]: value}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [optItem.field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [optItem.field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 选择校验规则
export const selectCheck = function (optItem) {
  const value = !optItem.isChecked
  this.checkList = multipleChecked(this.checkList, {id: optItem.id})
  // 更新右侧的值
  const item = {...this.item, [optItem.field]: value}
  this.$emit('setItem', item)
  // 更新整体表单的值
  let formData = searchCover(this.formData, {id: this.item.id}, v => ({...v, [optItem.field]: value}))
  formData = formData.map(v => ({...v, children: v.type == 'split' ? searchCover(v.children, {id: this.item.id}, v => ({...v, [optItem.field]: value})) : v.children}))
  this.$emit('setFormData', formData)
}
// 单选
export const selectItem = function (item) {
  const options = radioChecked(this.item.options, {id: item.id})
  this.changeField(options, 'options')
}
// 删除当前条目
export const delItem = function (item) {
  const options = this.item.options.filter(v => v.id != item.id).map((v, index) => ({...v, id: index + 1}))
  this.changeField(options, 'options')
}
// 改变拖拽
export const changeDrag = function (e) {
  // console.log(e)
  // console.log(this.formData)
  this.changeField(this.item.options.map((v, index) => ({...v, id: index + 1})), 'options')
}
// 添加新选项
export const addOptions = function () {
  const options = [...this.item.options, {id: `radio_${guID()}`, name: '新选项', isShowLink: false, type: '', link: ''}].map((v, index) => ({...v, id: index + 1}))
  this.changeField(options, 'options')
}
// 重置选择
export const resetSelect = function () {
  const options = this.item.options.map(v => ({...v, isChecked: false}))
  this.changeField(options, 'options')
}
// 显示关联字段控制
export const showLink = function (item) {
  const options = searchCover(this.item.options, {id: item.id}, v => ({...v, isShowLink: !v.isShowLink}))
  this.changeField(options, 'options')
}
// 显示输入框
export const showInput = function (curOpt) {
  let options = this.item.options
  options = searchCover(options, {id: curOpt.id}, v => ({...v, isNeedOther: !v.isNeedOther}))
  this.changeField(options, 'options')
}
// 禁用字段
export const changeOptType = function (item, type) {
  const options = searchCover(this.item.options, {id: item.id}, v => ({...v, type}))
  this.changeField(options, 'options')
}
// 调整关联字段
export const changeLink = function (e, item) {
  const value = isString(e) || isNumber(e) ? e : e.target.value
  const options = searchCover(this.item.options, {id: item.id}, v => ({...v, link: value}))
  this.changeField(options, 'options')
}
// 调整选项
export const changeOptions = function (e, curOpt) {
  const value = isString(e) || isNumber(e) || isArray(e) ? e : e.target.value
  const options = searchCover(this.item.options, {id: curOpt.id}, v => ({...v, name: value}))
  this.changeField(options, 'options')
}